<template>
	<view class="content-img">
		<eui-nav-bar title="设置" color="#000" rightIcon="" />
	</view>
	<view class="main-view font-size-28" :style="{ width: scrollWidth, height: scrollHeight }" :class="[theme]">
		<view class="padding-all-28 background-color-fff border-radius-20">
			<view>主题</view>
			<view class="margin-top-20 flex main-body">
				<view class="main-theme" v-for="item in themeList" :key="item.c_value" :class="{
		'main-theme-1': item.c_value === 'theme-blue',
		'main-theme-2': item.c_value === 'theme-orangeRed'
	}" @click="handleTheme(item.c_value)">
					<template v-if="item.c_value == theme">
						<view class="main-theme-sel"></view>
						<eui-uicons class="main-theme-icon" type="checkmarkempty" size="28" />
					</template>
				</view>
			</view>
		</view>
		<view class="margin-top-20 color-888 font-size-26">订单设置</view>
		<view class="overflow-hidden background-color-fff border-radius-20 margin-top-12 padding-left-28">
			<eui-card-input title="推送方式" placeholder="" type="selector" alignType="right" rightIconType="right"
				:range="pushModeList" @buttonClick="selectPushMode"
				style="padding-left: 0" v-model="pushMode" />
			<eui-card-input title="打印设置" placeholder="" alignType="right" rightIconType="right" displayable
				style="padding-left: 0" @click="openPrintSetting" />
			<eui-card-input title="提醒设置" placeholder="" alignType="right" rightIconType="right" displayable
				style="padding-left: 0" @click="openRemindSetting" :showBottomLine='false' />
		</view>
		<view class="margin-top-20 color-888 font-size-26">其他设置</view>
		<view class="overflow-hidden background-color-fff border-radius-20 margin-top-12 padding-left-28">
			<view class="msg-warp flex-row-between">
				<view>消息推送</view>
				<view class="right-btn flex-row-center" @click="clickRight('push')">
					<span class="msg-prompt">{{ c_switchPush }}</span>
					<eui-uicons type="right" size="32" style="height: 16px;" />
				</view>
			</view>
			<!-- <eui-card-input v-model="c_switch" title="清理缓存" placeholder="2222" type="text" alignType="right" displayable
				 style="padding-right: 10px;" :showBottomLine='false' /> -->
		</view>
		<view class="setting-logOut margin-top-60" :class="[theme]" @click="onClickOut">注销登录</view>
	</view>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { ref, watch } from 'vue';
import { onLoad, onReady, onShow } from '@dcloudio/uni-app';
import { useMainStore } from '../../../store/modules/store';
import { useRefreshStore } from '@/store/useRefreshStore';
import apps from '../app';
import { useThemeStore } from '@/store/themeStore';
import BcmEventType from "@/pages/BcmViews/common/BcmEventType";
const themeStore = useThemeStore(); // 主题
const { themeList, theme } = storeToRefs(themeStore)
const mainStore = useMainStore();
const pushModeList = [
  {text: '定时', value: '1'},
  {text: '实时', value: '2'}
];

const pushMode = ref('2');
const c_switchPrint = ref(false);
const c_switchSound = ref(false);
const c_switchPush = ref('去开启');
const scrollWidth = ref(uni.getSystemInfoSync().windowWidth - 20 + 'px');
const scrollHeight = ref(uni.getSystemInfoSync().windowHeight + 'px');

const openPrintSetting = () => {
	uni.navigateTo({
		url: './setting/printSetting',
	});
}
const openRemindSetting = () => {
	uni.navigateTo({
		url: './setting/remindSetting',
	});
}

const selectPushMode = (e) => {
  uni.setStorageSync('pushMode', e);
  uni.$emit(BcmEventType.SWITCH_STORE, {msg: '初始化连接'});
}

watch(c_switchPrint, (newValue, oldValue) => {
	apps.setStorageSync('thisPrintingList', newValue == true ? '打印' : '不打印');
})
watch(c_switchSound, (newValue, oldValue) => {
	apps.setStorageSync('thissoundList', newValue == true ? '提醒' : '不提醒');
})
const clickRight = item => {
	// #ifdef APP-PLUS  
	if (plus.os.name == 'Android') { // 判断是Android
		var main = plus.android.runtimeMainActivity();
		var pkName = main.getPackageName();
		var uid = main.getApplicationInfo().plusGetAttribute("uid");
		var Intent = plus.android.importClass('android.content.Intent');
		var Build = plus.android.importClass("android.os.Build");
		//android 8.0引导  
		if (Build.VERSION.SDK_INT >= 26) {
			var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
			intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
		} else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0  
			var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
			intent.putExtra("app_package", pkName);
			intent.putExtra("app_uid", uid);
		} else { //(<21)其他--跳转到该应用管理的详情页  
			intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
			var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
			intent.setData(uri);
		}
		// 跳转到该应用的系统通知设置页  
		main.startActivity(intent);
	} else if (plus.os.name == 'iOS') { // 判断是ISO
		var app = plus.ios.invoke('UIApplication', 'sharedApplication');
		var app = plus.ios.invoke('UIApplication', 'sharedApplication');
		var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
		plus.ios.invoke(app, 'openURL:', setting);
		plus.ios.deleteObject(setting);
		plus.ios.deleteObject(app);
	}
	// #endif  
}
const onClickOut = () => {
	if (mainStore.forcedLogin) {
		uni.reLaunch({
			url: '../login/login',
		});
	}
	const refreshStore = useRefreshStore();
	refreshStore.reset();
	let loginData = apps.getStorageSync('login_data');
	let login_Data = {
		...loginData,
		sPassword: ''
	}
	const ObjectData = {
		sUser: loginData.sUser,
	}
	apps.setStorageSync('login_data', login_Data);
	apps.setStorageSync('uniIdToken', "");
	apps.MBMLogOut(ObjectData, (res) => {
    uni.$emit(BcmEventType.SWITCH_STORE,{msg:'初始化连接'});
	}, (err) => {
		apps.setStorageSync('login_data', login_Data);
		apps.setStorageSync('uniIdToken', "");
		console.log(err);
	});
}
const GetAppAuthorize = (count) => {
	let Authorize = uni.getAppAuthorizeSetting();
	if (Authorize && Authorize.notificationAuthorized) {
		c_switchPush.value = (Authorize.notificationAuthorized == 'authorized' ? '已开启' : '去开启');
	}
	if (count > 0) {
		count--;
		setTimeout(function () {
			GetAppAuthorize(count)
		}, 1000);
	}
}
const handleTheme = (type: string) => {
	console.log(type);
	if (type === theme.value) {
		return
	}
	uni.showModal({
		title: ' 提示',
		content: '切换主题后将重新启动APP, 请确认是否继续?',
		showCancel: true,
		success: async (res) => {
			if (res.confirm) {
				themeStore.toggleTheme(type);
				uni.showLoading({ title: '正在重启, 请勿操作屏幕...', mask: true });
				setTimeout(() => {
					uni.hideLoading();
					// #ifdef APP
					plus.runtime.restart();
					// #endif

					// TODO 调试模式, 有bug, pinia State未更新选择后的ip 后期删除
					// #ifndef APP
					uni.reLaunch({
						url: '/pages/MainViews/index/index'
					});
					// #endif
				}, 1000)
			}
		}
	});
}
onReady(() => {
	uni.getSystemInfo({
		success: function (res) {
			const userInfoPage = uni.createSelectorQuery().select(".content-img");
			userInfoPage.boundingClientRect(function (data) {
				scrollHeight.value = (res.windowHeight - data.height - 20) + 'px';
			}).exec()
		}
	});
})
onShow(() => {
	console.log("onShow: " + JSON.stringify('onShow'));
	GetAppAuthorize(5);
})
onLoad(() => {
	if (apps.getStorageSync('thisPrintingList')) {
		c_switchPrint.value = (apps.getStorageSync('thisPrintingList') == '打印') ? true : false;
	} else {
		c_switchPrint.value = false;
	}
	if (apps.getStorageSync('thissoundList')) {
		c_switchSound.value = (apps.getStorageSync('thissoundList') == '提醒') ? true : false;
	} else {
		c_switchSound.value = false;
	}

	if (!!uni.getStorageSync('pushMode')) {
		pushMode.value = uni.getStorageSync('pushMode')
	} else {
		uni.setStorageSync('pushMode', '2')
		pushMode.value = '2'
	}
})
</script>

<style lang="scss" scoped>
@import "@/main.scss";

.content-img {
	width: 100%;
	background-color: #fff;
	background-repeat: no-repeat;
}

.main-view {
	flex-direction: column;
	flex: 1;
	padding: 20rpx;
	background-color: #f2f2f2;
}

.msg-prompt {
	margin-right: 12rpx;
	color: var(--theme-color) !important;
	font-size: 24rpx;
}

.msg-warp {
	padding: .75rem 10px .75rem 0;

	.right-btn {
		height: 32rpx;
		padding-right: 4px;
	}
}

.flex {
	display: flex;
}

.font-size-26 {
	font-size: 26rpx;
}

.font-size-28 {
	font-size: 28rpx;
}

.setting-logOut {
	display: flex;
	justify-content: center;
	align-items: center;
	width: 100%;
	height: 100rpx;
	color: var(--theme-color);
	font-size: 28rpx;
	background-color: #fff;
	border-radius: 20rpx;
}

.padding-all-28 {
	padding: 28rpx;
}

.background-color-fff {
	background-color: #fff;
}

.border-radius-20 {
	border-radius: 20rpx;
}

.margin-top-20 {
	margin-top: 20rpx;
}

.margin-top-60 {
	margin-top: 60rpx;
}

.margin-top-12 {
	margin-top: 12rpx;
}

.color-888 {
	color: rgba(136, 136, 136, 1);
}

.main-theme {
	position: relative;
	width: 80rpx;
	height: 80rpx;
	margin-right: 32rpx;
	border-radius: 16rpx;
}

.main-theme-1 {
	background-color: rgba(2, 183, 230, 1);
}

.main-theme-2 {
	background-color: #ff6430;
}

.main-body .main-theme:last-child {
	margin-right: 0rpx;
}

.main-theme-sel {
	position: absolute;
	right: 0;
	bottom: 0;
	width: 25px;
	height: 25px;
	clip-path: polygon(100% 10%, 100% 100%, 10% 100%);
	background: #fff;
}

.main-theme-icon {
	position: absolute;
	right: -1px;
	bottom: -1px;
	color: var(--theme-color) !important;
}

.overflow-hidden {
	overflow: hidden;
}

.padding-left-28 {
	padding-left: 28rpx;
}

::v-deep .m-title-text {
	color: #000 !important;
}

::v-deep .m-input-input {
	color: rgba(2, 183, 230, 1) !important;
}
::v-deep .uni-select__input-text{
	font-size: 24rpx;
	color: #888;
}
</style>